
<template>
  <div>
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row style="height:60px">
            <el-button size="small" type="primary" icon="el-icon-plus" @click="showDialog=true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list">
            <el-table-column type="index" label="序号" />
            <el-table-column align="center" width="240px" prop="name" label="角色名称" />
            <el-table-column align="center" width="440px" prop="description" label="描述" />
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="small">分配权限</el-button>
                <el-button type="success" size="small" @click="editRow(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="delRow(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <br>
          <!-- 分页器 -->
          <el-pagination
            :current-page.sync="page.page"
            :page-sizes="[2, 3, 4,10]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="sizeChange"
            @current-change="currentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog
      :title="roleForm.id ? '编辑角色': '新增角色'"
      :visible.sync="showDialog"
      width="40%"
      close="reset"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="80px" :inline="false" size="normal">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="small" @click="showDialog=false">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import { getRoleList, getCompanyInfo, addRole, updateRole, deleteRole } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      list: [],
      formData: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在2到8', trigger: 'blur' }

        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在2到20', trigger: 'blur' }

        ]

      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async  getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    sizeChange(val) {
      this.page.pagesize = val
      this.getRoleList()
    },
    currentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    async btnOk() {
      try {
        await this.$refs.roleForm.validate()
      } catch (error) {
        return console.log(error)
      }
      if (this.roleForm.id) {
        await updateRole(this.roleForm)
      } else {
        await addRole(this.roleForm)
      }

      this.$message.success('操作成功')
      this.showDialog = false
      this.getRoleList()
    },
    reset() {
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    editRow(val) {
      this.roleForm = { ...val }
      this.showDialog = true
    },
    async delRow(val) {
      try {
        await this.$confirm('确定删除吗')
      } catch (error) {
        return console.log(error)
      }
      await deleteRole(val.id)
      this.$message.success('删除成功')
      this.getRoleList()
    }

  }

}
</script>

<style>

</style>

